<?php

namespace app\controllers;

use Yii;
use app\models\GkyGroupMsg;
use app\models\GkyGroupMsgSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\GkyWeAccountSearch;
use Overtrue\Wechat\Media;

/**
 * GkyGroupMsgController implements the CRUD actions for GkyGroupMsg model.
 */
class GkyGroupMsgController extends Controller
{
     public $layout='main_1';
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all GkyGroupMsg models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new GkyGroupMsgSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single GkyGroupMsg model.
     * @param string $id
     * @return mixed
     */
    public function actionSend($id)
    {
        return $this->render('send', [
            'model' => $this->findModel($id),
        ]);
    }
    /**
     * Creates a new GkyGroupMsg model.
     * If creation is successful, the browser will be redirected to the 'send' page.
     * @return mixed
     */
    public function actionCreate(){
       $model = new GkyGroupMsg();
        $GkyGroupMsg = new GkyWeAccountSearch();
        if($model->load(Yii::$app->request->post())){
           $weid=$GkyGroupMsg->findBySql('select * from gky_we_account where wename="'.$model->wename.'"')->one(); 
            $model->weid=$weid->id;
            $model->addtime=date('y-m-d h:i:s',time());
            $model->sendtime=0;
            $model->sendcount=0;
            $media = new Media($weid->weappid, $weid->wesecret);
             $filename = $_FILES['file']['tmp_name'];
            $destination="D:/www/gky/web/img/upload_temp.jpg";
            move_uploaded_file($filename, $destination);
            $mediaget=$media->forever()->image($destination);
            $media->mediaid=$mediaget['media_id'];;
            $media->imageurl=$mediaget['url'];
            if ( $model->save()) {
            return $this->redirect(['send', 'id' => $model->id]);
            }
        }else {
            return $this->render('create', [
                'model' => $model,
                'GkyGroupMsg'=>$GkyGroupMsg,
            ]);
        }
    
    }
    /**
     * Updates an existing GkyGroupMsg model.
     * If update is successful, the browser will be redirected to the 'send' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

         $GkyGroupMsg = new GkyWeAccountSearch();
        if($model->load(Yii::$app->request->post())){
         //保存图片地址
            $files = Yii::$app->request->post('files');
//            $fileNames = Yii::$app->request->post('fileNames');
            $fileStatus = Yii::$app->request->post('fileStatus');
            for($i = 0 ; $i < count($fileStatus);$i++){
                if($fileStatus[$i] == 1){
                    $model->media_id = $files[$i];
                }
            } 
            $weid=$GkyGroupMsg->findBySql('select id from gky_we_account where wename="'.$model->wename.'"')->one();
            $model->weid=$weid->id;
            if ( $model->save()) {
            return $this->redirect(['send', 'id' => $model->id]);
            }
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing GkyGroupMsg model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the GkyGroupMsg model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return GkyGroupMsg the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = GkyGroupMsg::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
